package com.allqj.sfx.infrastructure.repository;

import com.allqj.sfx.domain.entity.SystemResource;
import com.allqj.sfx.domain.repository.SystemResourceRepository;
import com.allqj.sfx.infrastructure.converter.SystemResourceConverter;
import com.allqj.sfx.infrastructure.dao.SystemResourceDAO;
import com.allqj.sfx.infrastructure.po.SystemResourceDO;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author sjw
 * @className SystemResourceRepositoryImpl
 * @date 2022/7/28 11:20
 * @description: SystemResourceRepository具体实现
 * Repository接口定义位于domain层
 * Repository接口具体实现位于infrastructure层
 * 与底层db解耦
 */
@Repository
public class SystemResourceRepositoryImpl implements SystemResourceRepository {
    private final SystemResourceDAO systemResourceDAO;

    public SystemResourceRepositoryImpl(SystemResourceDAO systemResourceDAO) {
        this.systemResourceDAO = systemResourceDAO;
    }

    @Override
    public List<SystemResource> sonSystemResources(Integer fatherId) {
        List<SystemResourceDO> systemResourceDOS = systemResourceDAO.findAllByFatherIdAndIsdeleteFalse(fatherId);
        if (systemResourceDOS == null) return null;
        return systemResourceDOS.stream().map(SystemResourceConverter.INSTANCE::do2Entity).collect(Collectors.toList());
    }

    @Override
    public List<SystemResource> systemResources(List<Integer> ids) {
        List<SystemResourceDO> systemResourceDOS = systemResourceDAO.findAllByIdInAndIsdeleteFalse(ids);
        if (systemResourceDOS == null) return null;
        return systemResourceDOS.stream().map(SystemResourceConverter.INSTANCE::do2Entity).collect(Collectors.toList());
    }
}
